﻿Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class CodigoDeBarra
    Public Shared Sub REGGerarCodigoBarra(ByVal strCodigo As String, ByVal strPathNome As String)
        Const intFino As Integer = 1
        Const intLargo As Integer = 3
        Const intAltura As Integer = 50

        Dim intLarguraFinal As Integer = 405
        Dim intAlturaFinal As Integer = 50
        Dim intAtual As Integer = 0
        Dim intI As Integer
        Dim intF As Integer
        Dim intF1 As Integer
        Dim intF2 As Integer
        Dim strF As String
        Dim strTexto As String



        Dim strCodigoBarra As String() = New String(99) {}
        Dim dimensaoFinal As New Size(intLarguraFinal, intAlturaFinal)
        Dim oIMGFinal As Image = New Bitmap(intLarguraFinal, intAlturaFinal, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim oBPreto As New SolidBrush(Color.Black)
        Dim oBBranco As New SolidBrush(Color.White)
        Dim oGrap As Graphics = Graphics.FromImage(oIMGFinal)



        oGrap.FillRectangle(oBBranco, New Rectangle(0, 0, intLarguraFinal, intAlturaFinal))


        strCodigoBarra(0) = "00110"
        strCodigoBarra(1) = "10001"
        strCodigoBarra(2) = "01001"
        strCodigoBarra(3) = "11000"
        strCodigoBarra(4) = "00101"
        strCodigoBarra(5) = "10100"
        strCodigoBarra(6) = "01100"
        strCodigoBarra(7) = "00011"
        strCodigoBarra(8) = "10010"
        strCodigoBarra(9) = "01010"


        For intF1 = 9 To 0 Step -1
            For intF2 = 9 To 0 Step -1
                intF = intF1 * 10 + intF2
                strTexto = ""
                For intCont As Integer = 0 To 4
                    strTexto += strCodigoBarra(intF1).Substring(intCont, 1) & strCodigoBarra(intF2).Substring(intCont, 1)
                Next
               strCodigoBarra(intF) = strTexto
            Next
        Next


        oGrap.FillRectangle(oBPreto, New Rectangle(0, 0, intFino, intAltura))
        intAtual += intFino
        oGrap.FillRectangle(oBBranco, New Rectangle(intAtual, 0, intFino, intAltura))
        intAtual += intFino
        oGrap.FillRectangle(oBPreto, New Rectangle(intAtual, 0, intFino, intAltura))
        intAtual += intFino
        oGrap.FillRectangle(oBBranco, New Rectangle(intAtual, 0, intFino, intAltura))
        intAtual += intFino

        strTexto = strCodigo



        If (strTexto.Length Mod 2) <> 0 Then
            strTexto = "0" & strTexto
        End If



        While strTexto.Length > 0
            intI = Convert.ToInt32(strTexto.Substring(0, 2))
            strTexto = UtilPadrao.conversao.InverteString(UtilPadrao.conversao.InverteString(strTexto).Substring(0, strTexto.Length - 2))
            strF = strCodigoBarra(intI)



            Dim intCont As Integer = 0
            While intCont <= 9
                If strF.ToString().Substring(intCont, 1) = "0" Then
                    intF1 = intFino
                Else
                    intF1 = intLargo
                End If



                oGrap.FillRectangle(oBPreto, New Rectangle(intAtual, 0, intF1, intAltura))
                intAtual += intF1

                If strF.ToString().Substring(intCont + 1, 1) = "0" Then
                    intF2 = intFino
                Else
                    intF2 = intLargo
                End If

                oGrap.FillRectangle(oBBranco, New Rectangle(intF2, 0, intFino, intAltura))

                intAtual += intF2
                intCont = intCont + 2

            End While
        End While



        oGrap.FillRectangle(oBPreto, New Rectangle(intAtual, 0, intLargo, intAltura))
        intAtual += intLargo
        oGrap.FillRectangle(oBBranco, New Rectangle(intAtual, 0, intFino, intAltura))
        intAtual += intFino
        oGrap.FillRectangle(oBPreto, New Rectangle(intAtual, 0, 1, intAltura))
        intAtual += 1


        oIMGFinal.Save(strPathNome, System.Drawing.Imaging.ImageFormat.Jpeg)
        oIMGFinal.Dispose()

    End Sub

End Class

